package com.dkd.rollback;

import cn.elegent.actx.annotation.RollBack;
import cn.elegent.actx.context.Snapshot;
import cn.elegent.actx.core.rollback.RollBackHandler;
import com.dkd.config.ACTXConfig;
import com.dkd.contract.VendoutContract;
import com.dkd.entity.ChannelEntity;
import com.dkd.service.ChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 扣减库存的回滚类
 * @author DiXiaoNie
 * @date 2023/02/05
 * @since 2023/2/5 17:55
 */
@RollBack(mainName = ACTXConfig.ORDER_VENDOUT, childName = ACTXConfig.ORDER_VENDOUT_DED)
@Slf4j
public class VendoutRollBack implements RollBackHandler {

    @Autowired
    private ChannelService channelService;
    /**
     * 出货失败 设备更新出货日志 恢复库存
     * @param topic
     * @param snapshot
     */
    @Override
    public void doRollBack(String topic, Snapshot snapshot) throws Exception {
        log.info("收到了回滚的消息来自：" + topic + " ,数据：" + snapshot.getData());
        //提取快照
        VendoutContract vendoutContract = snapshot.findData("vendoutContract", VendoutContract.class);
        //快照中没有信息直接返回
        if (vendoutContract == null) {
            return;
        }
        log.info("回滚库存");
        String innerCode = vendoutContract.getInnerCode();
        String channelCode = vendoutContract.getVendoutData().getChannelCode();
        ChannelEntity channelEntity = this.channelService.getChannelInfo(innerCode, channelCode);
        //更新货道库存
        channelEntity.setCurrentCapacity(channelEntity.getCurrentCapacity() + 1);
        this.channelService.updateById(channelEntity);
    }
}
